package com.example.yiwei.crm.view.activity.jdjycooractivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao.managerDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.dao_impl.managerDaoImpl;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class JDJYSeacherCustomerActivity extends AppCompatActivity {
    ImageView img_commit;
    private TextView txt_create_time1,txt_create_time2;
    private EditText edit_name,edit_phone;
    private RadioGroup rg;
    private RadioButton rb1,rb2;
    private Button btn_show1,btn_show2;
    private List<CustomerMessageEntity> alist;
    private ArrayAdapter adapter1,adapter2;
    private TimePickerView mpv;
    private TimePickerView mpv1;
    private String create_time1,create_time2;
    private List<String> name_list=new ArrayList<>();
    List<managerEntity> managerList;
    private Spinner spinner_manager;
    int manager_id;
    SharedPreferences sp;
    int sex;
    Date date1,date2;
    String managerName;
    List<dockingEntity> coorlist= new ArrayList<>();
    private List<String> coor_name_list = new ArrayList<>();
    private LinearLayout linearLayout;
    private ImageView img_luru,img_find,img_me,img_tj,img_back;
    private ImageView img_qk,img_qk1,img_qk2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jdjyseacher_customer);
        img_qk= (ImageView) findViewById(R.id.qingkong);
        img_qk1= (ImageView) findViewById(R.id.qingkong1);
        img_qk2= (ImageView) findViewById(R.id.qingkong2);
        spinner_manager= (Spinner) findViewById(R.id.coording_customer_search_spinner_manager);
        btn_show1= (Button) findViewById(R.id.coording_customer_search_btn_show1);
        btn_show2= (Button) findViewById(R.id.coording_customer_search_btn_show2);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_commit= (ImageView) findViewById(R.id.coording_customer_search_img_commit);
        txt_create_time1= (TextView) findViewById(R.id.coording_customer_search_txt_create_time1);
        txt_create_time2= (TextView) findViewById(R.id.coording_customer_search_txt_create_time2);
        edit_name= (EditText) findViewById(R.id.coording_customer_search_edit_name);
        edit_phone= (EditText) findViewById(R.id.coording_customer_search_edit_mobile_phone);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        linearLayout= (LinearLayout) findViewById(R.id.search_linearLayout);
        rg= (RadioGroup) findViewById(R.id.coording_customer_search_rg);
        rb1= (RadioButton) findViewById(R.id.coording_customer_search_rb_men);
        rb2= (RadioButton) findViewById(R.id.coording_customer_search_rb_women);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 = date;
                create_time1=getTime(date);
                txt_create_time1.setText(create_time1);
            }
        });
        btn_show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv1.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                create_time2=getTime(date);
                txt_create_time2.setText(create_time2);
            }
        });
        btn_show2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv1.show();
            }
        });
        img_qk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt_create_time1.setText("");
            }
        });
        img_qk1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt_create_time2.setText("");
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==rb1.getId()){
                    sex=1;
                }else{
                    sex=2;
                }
            }
        });
        img_qk2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rg.clearCheck();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                String json = object.toString();
                dockDao dockDao = new dockDaoImpl();
                coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json1 = object.toString();
                managerDao manaDao=new managerDaoImpl();
                managerList=manaDao.queryAllManager(baseClass.query_manager_url,json1);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (managerList!=null){
                            name_list.add(0,"");
                            for (int i2=0;i2<managerList.size();i2++){
                                name_list.add(managerList.get(i2).getManager_name());
                            }
                            adapter1 = new ArrayAdapter(JDJYSeacherCustomerActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, name_list);
                            adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                            spinner_manager.setAdapter(adapter1);
                            spinner_manager.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                    managerName=spinner_manager.getSelectedItem().toString();
                                    for (int i1= 0;i<managerList.size();i++){
                                        if (managerName.equals(managerList.get(i1).getManager_name())){
                                            manager_id = Integer.valueOf(managerList.get(i1-1).getManager_id());
                                        }
                                    }
                                }
                                @Override
                                public void onNothingSelected(AdapterView<?> adapterView) {

                                }
                            });
                        }
                    }
                });
            }
        }).start();
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JDJYSeacherCustomerActivity.this, JDJYCoorActivity.class);
                intent.putExtra("lr","lr");
                startActivity(intent);
                finish();
            }
        });
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JDJYSeacherCustomerActivity.this, JDJYCoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JDJYSeacherCustomerActivity.this, JDJYCoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JDJYSeacherCustomerActivity.this, JDJYCoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JDJYSeacherCustomerActivity.this,JYJDCustomerActivityActivity.class);
                intent.putExtra("KAA09",manager_id);
                intent.putExtra("KAA04",sex);
                intent.putExtra("KAA02",edit_name.getText().toString());
                intent.putExtra("KLA06",edit_phone.getText().toString());
                intent.putExtra("CTS01A",txt_create_time1.getText().toString());
                intent.putExtra("CTS01B",txt_create_time2.getText().toString());
                startActivity(intent);
                finish();
            }
        });
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
